import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution1807 {
    public String evaluate(String s, List<List<String>> knowledge) {
        Map<String,String> map=new HashMap<>();
        for (List<String> list : knowledge) {
            map.put(list.get(0),list.get(1));
        }
        StringBuilder ans=new StringBuilder();
        StringBuilder now=new StringBuilder();
        int state=0;
        for (char c : s.toCharArray()) {
            if (c!='(' && c!=')'){
                if (state==0){
                    ans.append(c);
                }else {
                    now.append(c);
                }
            }else if (c=='('){
                state=1;
            }else {
                state=0;
                String p=now.toString();
                ans.append(map.getOrDefault(p,"?"));
                now=new StringBuilder();
            }
        }
        return ans.toString();
    }

    public static void main(String[] args) {
        System.out.println(new Solution1807().evaluate("(name)is(age)yearsold", Arrays.asList(Arrays.asList("name","bob"),Arrays.asList("age","two"))));
    }
}
